package br.com.healthapplet.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import br.com.healthapplet.model.Result;
import br.com.healthapplet.model.TemplateResult;

import com.google.gson.Gson;

public class ServerUtil {

	public boolean callRegisterServicewithPostMethod(String finger, String idPaciente, String encoded, String url) throws IOException{
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);

		List<NameValuePair> params = new ArrayList<NameValuePair>(3);
		params.add(new BasicNameValuePair("finger", finger));
		params.add(new BasicNameValuePair("idPaciente", idPaciente));
		params.add(new BasicNameValuePair("encoded", encoded));
		
		httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
		
		HttpResponse response =  httpclient.execute(httppost);
		
		BufferedReader rd = new BufferedReader(
		        new InputStreamReader(response.getEntity().getContent()));
		
		String line = rd.readLine();
		
		rd.close();
		
		Result p = new Result();
		p = deserializer(line);
		
		return p.isResult();
	}
	
	public void callConfirmBiometriaCompareServer(String idConsulta, String url) throws IOException{
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);

		List<NameValuePair> params = new ArrayList<NameValuePair>(2);
		params.add(new BasicNameValuePair("verify", "true"));
		params.add(new BasicNameValuePair("idConsulta", idConsulta));
		
		httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
		
		HttpResponse response =  httpclient.execute(httppost);
	}
	
	public byte[] callBiometriaTemplateGetter(String finger, String idConsulta, String url) throws IOException{
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);

		List<NameValuePair> params = new ArrayList<NameValuePair>(2);
		params.add(new BasicNameValuePair("finger", finger));
		params.add(new BasicNameValuePair("idConsulta", idConsulta));
		
		httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

		HttpResponse response =  httpclient.execute(httppost);
		
		BufferedReader rd = new BufferedReader(
		        new InputStreamReader(response.getEntity().getContent()));
		
		String line = rd.readLine();
		
		rd.close();
		
		return stringDeserializer(line);
	}
	
	private Result deserializer(String result){
		Result res = new Result();
		Gson gson = new Gson();
		res = gson.fromJson(result, Result.class);
		
		return res;
	}
	
	private byte[] stringDeserializer (String result){
//		TemplateResult res = new TemplateResult();
		Gson gson = new Gson();
		byte[] res= gson.fromJson(result, byte[].class);
		
		return res;
	}
	
}
